/**
 * 
 */
package testDAO;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Ignore;
import org.junit.Test;

import de.hdu.mfz.dao.RouteDAO;
import de.hdu.mfz.dao.UserDAO;
import de.hdu.mfz.model.Route;
import de.hdu.mfz.model.Routenpunkt;
import de.hdu.mfz.model.User;



/**
 * @author Katharina
 * Methoden: insert, delete, findById, findAll, findByFahrer, findRoute
 */
public class RouteDAOTest_neu extends AbstractDataAccessTest {
	
	private RouteDAO routeDao;
	private UserDAO userDao;
	private String tableRoute = "Route";
	private String tableUser = "User";
	private String tableRoutenpunkt = "Routenpunkt";
	User u1, u2, u3;
	Route r1, r2, r3, r4, r5;
	
	public void setRouteDao(RouteDAO routeDao) {
		this.routeDao = routeDao;
	}
	
	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}
	
	/**
	 * Hilfsmethode
	 */
	private void createEverything() {
		// create two persons
		 u1 = new User("Test1", "Test1");
		 u2 = new User("Test2", "Test2");
		 u3 = new User("Test3", "Test3");
		
		userDao.insert(u1);
		userDao.insert(u2);
		userDao.insert(u3);
		
		 r1 = new Route(new Date(), new Date(), 10, u1);
		 r2 = new Route(new Date(), new Date(), 10, u2);
		 r3 = new Route(new Date(), new Date(), 10, u3);
		
		routeDao.insert(r1);
		routeDao.insert(r2);
		routeDao.insert(r3);
	}


	//Test insert
	@SuppressWarnings("deprecation")
	@Test
	public void testInsert(){
		  
		String[] tables = {tableRoute};
		deleteFromTables(tables);
		
		routeDao.insert(new Route(new Date(), new Date(), 10, new User("Test1", "Test1")));
		routeDao.insert(new Route(new Date(), new Date(), 20, new User("Test2", "Test2")));
		   
		int rows = countRowsInTable("Route");
		assertEquals("Die DB muss zwei Zeilen enthalten", 2, rows);
		   
		routeDao.insert(new Route(new Date(), new Date(), 30, new User("Test3", "Test3")));
		   
		rows = countRowsInTable("Route");
		assertEquals("Die DB muss drei Zeilen enthalten", 3, rows);
		deleteFromTables(tables);  
	}
	  
	  
	 
	 // Test delete 
	 // TODO: funktioniert nicht!!!
	  @Ignore 
	  @SuppressWarnings("deprecation")
	  @Test
	  public void testDelete(){
		  
		String[] tables = {tableRoute, tableUser};
		deleteFromTables(tables);
		
		createEverything();
		
		  int rows = countRowsInTable("Route");
		  Route delete = routeDao.findById(r2.getRouteId());
		  routeDao.delete(delete);
	   
		  rows = countRowsInTable("Route");
		  assertEquals("Die DB muss 2 Zeilen enthalten", 2, rows);
		  
		  deleteFromTables(tables);
	  }

	
	@Test 
	public void testFindAll() {
		
		String[] tables = {tableRoute, tableUser};
		deleteFromTables(tables);
		
		createEverything();
		User u4 = new User("Test4", "Test4");
		User u5 = new User("Test5", "Test5");
		userDao.insert(u4);
		userDao.insert(u5);
		
		
		List<Route> alleRouten = routeDao.findAll();
		int rows = countRowsInTable("Route");
		assertEquals("Die DB muss drei Zeile enthalten", 3, rows);
		
		routeDao.insert(new Route(new Date(), new Date(), 10, u4));
		routeDao.insert(new Route(new Date(), new Date(), 10, u5));

		alleRouten = routeDao.findAll();
		rows = countRowsInTable("Route");
		assertEquals("Die DB muss fünf Zeile enthalten", 5, rows);
		
		deleteFromTables(tables);
	}

	@Test
	public void testFindById() {

		String[] tables = {tableRoute};
		deleteFromTables(tables);
		
		createEverything();
		
		List<Route> alleRouten = routeDao.findAll();
		
		for (Route r : alleRouten){
			int id = r.getRouteId();
			
			Route found = routeDao.findById(id);
			assertEquals(r.getPlaetze(), found.getPlaetze());
			assertEquals(r.getStartzeit(), found.getStartzeit());
			assertEquals(r.getZielzeit(), found.getZielzeit());
			assertEquals(r.getFahrer().getVorname(), found.getFahrer().getVorname());
			assertEquals(r.getFahrer().getNachname(), found.getFahrer().getNachname());
		}
		
		deleteFromTables(tables);
	}
	
	
	@Test 
	public void testFindByFahrer() {
		
		String[] tables = {tableRoute, tableUser};
		deleteFromTables(tables);
			
		createEverything();
		
			List<Route> found = routeDao.findByFahrer(u1);
			for (Route r : found){
				assertEquals(r.getFahrer().getNachname(), u1.getNachname());
				assertEquals(r.getFahrer().getVorname(), u1.getVorname());
			}
			
			found = routeDao.findByFahrer(u3);
			for (Route r : found){
				assertEquals(r.getFahrer().getNachname(), u3.getNachname());
				assertEquals(r.getFahrer().getVorname(), u3.getVorname());
			}
			
			deleteFromTables(tables);
	}
	
	@Test 
	public void testFindByRoute() {
		
		String[] tables = {tableRoute, tableUser, tableRoutenpunkt};
		deleteFromTables(tables);
		createEverything();
		

		
		//1. Route
		r1.getListe().add(new Routenpunkt("Passau", 1, r1));
		r1.getListe().add(new Routenpunkt("Waldkirchen", 2, r1));
		r1.getListe().add(new Routenpunkt("Deggendorf", 3, r1));
		
		//2. Route
		r2.getListe().add(new Routenpunkt("Passau", 1, r2));
		r2.getListe().add(new Routenpunkt("Waldkirchen", 2, r2));
		r2.getListe().add(new Routenpunkt("Waldkirchen", 3, r2));
		
		//2. Route
		r3.getListe().add(new Routenpunkt("Deggendorf", 1, r3));
		r3.getListe().add(new Routenpunkt("München", 2, r3));
		
		createEverything();
		
		
		
		List<Route> found = routeDao.findRoute("Passau", "Deggendorf");
		assertEquals(found.size(), 1);
		found.clear();
		
		found = routeDao.findRoute("Passau", "Waldkirchen");
		assertEquals(found.size(), 2);
		found.clear();
		
		found = routeDao.findRoute("Deggendorf", "Passau");
		assertEquals(found.size(), 0);
		deleteFromTables(tables);
	}

}
